﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Diagnostics;

namespace PostIt
{
    public partial class AddTasks : PhoneApplicationPage
    {
        Tasks aTask;
        TasksDBContext db;
        string taskID;
        bool firstTimeSee = true;
        
        //Color convertor
        private static Dictionary<string, string> ColorNameHexString = new Dictionary<string, string>()
        {
        
            { "magenta", "#FFFF0097" },
            { "purple",  "#FFA200FF"},
            { "teal",    "#FF00ABA9" },
            { "lime",    "#FF8CBF26" },
            { "brown",   "#FFA05000" },
            { "pink",    "#FFE671B8" },
            { "orange",  "#FFF09609" },
            { "blue",    "#FF1BA1E2" },
            { "red",     "#FFE51400" },
            { "green",   "#FF339933" },
            { "mango",   "#FFF09609" },
        };
        private static Dictionary<string, string> ColorHexNameString = new Dictionary<string, string>()
        {
        
            {  "#FFFF0097", "magenta"},
            {  "#FFA200FF"  ,"purple"},
            {   "#FF00ABA9", "teal" },
            {   "#FF8CBF26", "lime"},
            {   "#FFA05000", "brown" },
            {   "#FFE671B8","pink" },

            {   "#FF1BA1E2" ,"blue"},
            {   "#FFE51400" ,"red" },
            {   "#FF339933", "green"},
            {   "#FFF09609", "mango"},
        };
        
        //ItemViewModel  ourItem ;
        MainViewModel mainViewModle = App.ViewModel;
        
        public AddTasks()
        {
            InitializeComponent();
            DataContext = ColorExtension.AccentColors();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {



            taskID = "";
            if (NavigationContext.QueryString.TryGetValue("taskId", out taskID) && firstTimeSee == true)
            {
                firstTimeSee = false;
                db = App.DB;
                int id = Int32.Parse(taskID);
                IQueryable<Tasks> taskQuery = from Tasks t in db.Items where t.TaskId == id select t;
                aTask = taskQuery.FirstOrDefault();


                txtTile.Text = aTask.tileText;
                txtTitle.Text = aTask.Title;
                txtDetail.Text = aTask.Detail;
                txtColor.SelectedItem = ColorHexNameString[aTask.backColor];
                DueDateBox.Value = aTask.DueDate;


            }
        }
       
        //to find a tile in home screen
        private ShellTile FindTile(string partOfUri)
        {
            ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(
                tile => tile.NavigationUri.ToString() == "/DetailsPage.xaml?" + partOfUri);

            return shellTile;
        }
        
        //save button click
        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {

            //if we are in edit mode 
            if (NavigationContext.QueryString.TryGetValue("taskId", out taskID))
            {

                aTask.Title = txtTitle.Text;
                aTask.tileText = txtTile.Text;
                aTask.CreationDate = DateTime.Now;
                aTask.Detail = txtDetail.Text;
                aTask.DueDate = (DateTime)DueDateBox.Value;
                aTask.backColor = ColorNameHexString[txtColor.SelectedItem.ToString()];
                App.DB.SubmitChanges();
                App.ViewModel.Items.Clear();
                App.ViewModel.LoadData();

                ShellTile aTile = FindTile("taskId=" + aTask.TaskId);

                if (aTile != null)
                {
                    aTile.Update(new StandardTileData()
                    {

                        Title = aTask.tileText,
                        BackgroundImage = new Uri("/images/tiles/" + aTask.backColor.Substring(1, 8) + ".png", UriKind.Relative),
                        //BackgroundImage = new Uri("isostore:/Shared/ShellContent/tile.jpg", UriKind.Absolute),
                        BackBackgroundImage = new Uri("/images/tiles/" + aTask.backColor.Substring(1, 8) + ".png", UriKind.Relative),
                        //  Count = 2,
                        BackContent = aTask.tileText,
                        BackTitle = aTask.CreationDate.ToShortDateString()

                    });

                }

                NavigationService.Navigate(new Uri("/DetailsPage.xaml?taskId=" + aTask.TaskId, UriKind.Relative));
                return;

            }


            //if we are in add task mode 
            db = App.DB;

            //create a Task
            Tasks t = new Tasks
            {
                isDone = false,
                isPin = false,
                Title = txtTitle.Text,
                tileText = txtTile.Text,
                CreationDate = DateTime.Now,
                Detail = txtDetail.Text,
                DueDate = (DateTime)DueDateBox.Value,
                backColor = ColorNameHexString[txtColor.SelectedItem.ToString()]

            };
            db.Items.InsertOnSubmit(t);
            db.SubmitChanges();
            //ourItem.TaskId = t.TaskId;
            mainViewModle.Items.Clear();
            mainViewModle.LoadData();



            // Navigate to the mainPage
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

      
    }
}